﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace STS.Account
{
    public partial class Login : System.Web.UI.Page
    {
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            Master.FindControl("NavigationMenu").Visible = false;
            Master.FindControl("NavigationMenuR").Visible = false;

            
        }

        protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
           
            
            
            if (sprawdzLoginHaslo(LoginUser.UserName, LoginUser.Password))
            {

                e.Authenticated = true;

                //Login1.Visible = false;
                //MessageLabel.Text = "Successfully Logged In";
            }
            else
            {
                e.Authenticated = false;
                
            }
        }
        //private bool ValidationFunction(string UserName, string Password)
        //{
        //    bool boolReturnValue = true;
        //    string strConnection = "Data Source=BZYLAK-DELL;Initial Catalog=MGR;Integrated Security=True";
        //    SqlConnection sqlConnection = new SqlConnection(strConnection);
        //    String SQLQuery = "SELECT login, password, id FROM login_pass";
        //    SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
        //    SqlDataReader Dr;
        //    sqlConnection.Open();
        //    Dr = command.ExecuteReader();
        //    while (Dr.Read())
        //    {
                
        //        if ((UserName == Dr["login"].ToString()) & (Password == Crypto.Decrypt(Dr["password"].ToString())))
        //        {

                    
        //            Main.haslo=Crypto.Decrypt(Dr["password"].ToString());
        //            boolReturnValue = true;
        //            Response.Cookies["id"].Value = Dr["id"].ToString();
        //            Role(Dr["id"].ToString());
        //            Response.Cookies["id"].Expires = DateTime.Now.AddMinutes(15.00);
        //        }
        //        Dr.Close();
        //        return boolReturnValue;
        //    }
        //    return boolReturnValue;
        //}

        private bool sprawdzLoginHaslo(string Login, string haslo)
        {
            baza db = new baza();
            var ud = db.login_passes.SingleOrDefault(fbu => fbu.login == Login);
            
            
            if (ud == null)
            {
                return false;
            }
            else{
                if(ud.password == Crypto.Encrypt(haslo)){
                    Main.haslo = Crypto.Decrypt(ud.password);
                    Role(ud.id.ToString());
                    Main.id = ud.id;
                    
                    Response.Cookies["id"].Value = ud.id.ToString();
                    return true;
                }
                return false;
            }
        

        }

        private void Role(string id)
        {
            string strConnection = "Data Source=BZYLAK-DELL;Initial Catalog=MGR;Integrated Security=True";
            SqlConnection sqlConnection = new SqlConnection(strConnection);
            String SQLQuery = "SELECT typ_lekarza, id_osrodka FROM dane_lekarzy where ID_pracownika="+id;
            SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
            SqlDataReader Dr;
            sqlConnection.Open();
            Dr = command.ExecuteReader();
            Dr.Read();
            Main.idroli = Convert.ToInt32(Dr["typ_lekarza"].ToString());
            Main.idosrodka = Convert.ToInt32(Dr["id_osrodka"].ToString());
            Dr.Close();
        }
    }
}
